package com.mall.order.mq.delayOrder;

import com.mall.order.OrderCoreService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CancelOrderRequest;
import com.mall.order.dto.CancelOrderResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Author  kkw
 * Date  2021/6/23 15:26
 * Version 1.0
 * Description :消费订单延迟消费者
 */
@Slf4j
@Component
public class DelayOrderConsumer {
    /**
     * 1.获取订单编号
     * 2.查询tb_order表中,订单的状态,
     * 3.如果订单没支付,取消订单状态
     * 4.还原锁定订单
     * 释放锁定库存
     *
     */
    private DefaultMQPushConsumer consumer;

    @Autowired
    private OrderCoreService orderCoreService;
    @Autowired
    private OrderMapper orderMapper;

    @Value("${mq.nameserver.address}")
    private String nameserverAddress;

    @PostConstruct
    public void init() {
        consumer=new DefaultMQPushConsumer("delay_order_consumer");
        Exception ex=null;

        consumer.setNamesrvAddr(nameserverAddress);
        try {
            consumer.subscribe("delay_order","*");
            //收取信息
            consumer.setMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                    //取消订单的业务逻辑
                    for (MessageExt msg : list) {
                        byte[] body = msg.getBody();
                        String orderId = new String(body, 0, body.length);

                        //首先查看订单的支付状态,如果已支付,就不进行任何操作
                        Order order = orderMapper.selectByPrimaryKey(orderId);
                        //如果是初始状态,就取消
                        if (OrderConstants.ORDER_STATUS_INIT == order.getStatus()) {
                            CancelOrderRequest request = new CancelOrderRequest(orderId);
                            CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(request);
                            if (OrderRetCode.SUCCESS.getCode().equals(cancelOrderResponse.getCode())) {
                                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                            }
                        }
                    }
                    return null;
                }
            });

            consumer.start();
        } catch (MQClientException e) {
            ex=e;
            e.printStackTrace();
            log.error("订单取消失败:"+ex.getMessage());
        }
    }

}
